package com.blindBox.back.controller.getDataController;

import com.blindBox.back.annotation.JwtToken;
import com.blindBox.back.commonResult.MsgBackToFront;
import com.blindBox.back.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.TimeUnit;

@RestController
@RefreshScope //支持Nacos的动态刷新功能。
public class BackDataNoticeController
{
    @Autowired
    private RedisUtils redisUtils;

    @Value("${notice.expireTime}")
    private Integer expireTime;

    //大key
    @Value("${notice.noticeID}")
    private String noticeId;

    //处理返回的公告
    @JwtToken
    @PostMapping("/get_notice_data")
    public MsgBackToFront get_notice_data(@RequestParam("title")String title,@RequestParam("notice")String notice)
    {
        //公告的发布时间
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:ss:mm");
        String format = now.format(pattern);
        //将公告缓存到redis中
        redisUtils.putIntoHash(noticeId,title+"&"+format,notice,expireTime, TimeUnit.DAYS);
        //处理成功
        return new MsgBackToFront(null,MsgBackToFront.HANDLE_SUCCESS);
    }

    //获取所有的公告
    @JwtToken
    @PostMapping("/get_all_notice")
    public MsgBackToFront get_all_notice()
    {
        List<Map<Object,Object>> res=new ArrayList<>();
        Set keys = redisUtils.getAllKeys(noticeId);
        for (Object key : keys)
        {
            Map<Object,Object> map=new HashMap<>();
            //第一个字段name :公告的名称
            map.put("name", ((String) key).split("&")[0]);
            //第二个字段获取公告的发布时间
            map.put("publish",((String) key).split("&")[1]);
            //第三个字段expire: 公告的过期时间
            map.put("expire",redisUtils.getExpire(noticeId)/86400+"天");
            //第四个字段info: 公告的详情
            map.put("info",redisUtils.getValueByKey(noticeId,(String) key));
            res.add(map);
        }
          return new MsgBackToFront(res,MsgBackToFront.HANDLE_SUCCESS);
    }

    //删除指定的公告
    @JwtToken
    @PostMapping("/del_notice_by_key")
    public MsgBackToFront del_target_notice(@RequestParam("key")String key)
    {
        redisUtils.delSmallKey(noticeId,key);
        return new MsgBackToFront(null,MsgBackToFront.HANDLE_SUCCESS);
    }

    //查询指定公告的信息
    @JwtToken
    @PostMapping("/get_target_notice")
    public MsgBackToFront get_target_notice(@RequestParam("key")String key)
    {
        //如果key为空，默认查询出所有
        //获取所有的key
        Set allKeys = redisUtils.getAllKeys(noticeId);
        //找到含有的key的字符串的所有值
        for (Object allKey : allKeys) {
            boolean contains = ((String) allKey).contains(key);
            if(!contains)
            {
                //不满足条件的剔除
                allKeys.remove(allKey);
            }
        }
        //返回满足条件的所有key和对应的值
        List<Map<Object,Object>> res=new ArrayList<>();
        for (Object KEY : allKeys)
        {
            Map<Object,Object> map=new HashMap<>();
            //第一个字段name :公告的名称
            map.put("name", ((String) KEY).split("&")[0]);
            //第二个字段获取公告的发布时间
            map.put("publish",((String) KEY).split("&")[1]);
            //第三个字段expire: 公告的过期时间
            map.put("expire",redisUtils.getExpire(noticeId)/86400+"天");
            //第四个字段info: 公告的详情
            map.put("info",redisUtils.getValueByKey(noticeId,(String) KEY));
            res.add(map);
        }
        return new MsgBackToFront(res,MsgBackToFront.HANDLE_SUCCESS);
    }
}
